//
//  ShareListCell.m
//  phmall
//
//  Created by admin on 2017/11/16.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ShareListCell.h"
#define IMGSIZE (SCREEN_WIDTH-40)/3
@implementation ShareListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
+ (instancetype)theShareCellWithTableView:(UITableView *)tableView{
    
    static NSString *ID = @"SetCell";
    ShareListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[ShareListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.contentView.backgroundColor=[UIColor whiteColor];

        self.head=[[UIImageView alloc]init];
        ViewBorderRadius(self.head, 3, 0.5, [UIColor colorWithHexString:@"#adadad"]);
        [self.contentView addSubview:self.head];
        self.head.image=[UIImage imageNamed:@"图片"];
        self.head.layer.cornerRadius = 5;
        
        self.name=[[UILabel alloc] init];
        self.name.text=@"玛丽苏";
        self.name.font=[UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.name];
        
        self.time=[[UILabel alloc] init];
        [self.contentView addSubview:self.time];
        self.time.font=[UIFont systemFontOfSize:13];
        self.time.textColor=[UIColor colorWithHexString:@"#c5c5c5"];
        self.time.text=@"发布时间:2017-08-17";
        

        self.title=[[UILabel alloc] init];
        [self.contentView addSubview:self.title];
        self.title.font=[UIFont systemFontOfSize:15];
        self.title.numberOfLines=0;
        self.title.text=@"获取指定宽度width,字体大小fontSize,字符串value的高度,字符串value的高度,字符串value的高度,字符串value的高度";
        
        
        self.text=[[UILabel alloc] init];
        [self.contentView addSubview:self.text];
        self.text.font=[UIFont systemFontOfSize:13];
        self.text.textColor=[UIColor colorWithHexString:@"#adadad"];
        self.text.numberOfLines=0;
        self.text.text=@"获取指定宽度width,字体大小fontSize,字符串value的高度,字符串value的高度,字符串value的高度,字符串value的高度,字符串value的高度";
        
        self.addFriends=[UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.addFriends];
        [self.addFriends setTitle:Localized(@"添加好友") forState:UIControlStateNormal];
        
        self.addFriends.titleLabel.font=[UIFont systemFontOfSize:13];
        [self.addFriends setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.addFriends setTitleColor:[UIColor colorWithHexString:@"#adadad"] forState:UIControlStateSelected];
        ViewBorderRadius(self.addFriends, 5, 1, [UIColor orangeColor]);
        self.addFriends.backgroundColor=[UIColor whiteColor];
        [self.addFriends addTarget:self action:@selector(addFriendsAction) forControlEvents:UIControlEventTouchUpInside];
    
        self.imgView=[[UIView alloc]init];
        [self.contentView addSubview:self.imgView];
        self.imgView.backgroundColor=[UIColor whiteColor];
        
        self.line=[[UIView alloc]init];
        [self.contentView addSubview:self.line];
        self.line.backgroundColor=[UIColor groupTableViewBackgroundColor];
        
        self.like=[UIButton buttonWithType:UIButtonTypeCustom];
        [self.like setImage:[UIImage imageNamed:@"喜欢点赞"] forState:UIControlStateSelected];
        [self.like setImage:[UIImage imageNamed:@"喜欢未点"] forState:UIControlStateNormal];
        self.like.titleLabel.font=[UIFont systemFontOfSize:13];
        [self.like setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [self.like setTitleColor:[UIColor colorWithHexString:@"#adadad"] forState:UIControlStateNormal];
        [self.like addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
       
        [self.contentView addSubview:self.like];
        
        self.purchase=[UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.purchase];
        [self.purchase setTitle:Localized(@"去购买") forState:UIControlStateNormal];
        self.purchase.titleLabel.font=[UIFont systemFontOfSize:12];
        self.purchase.backgroundColor=[UIColor orangeColor];
        [self.purchase setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        ViewBorderRadius(self.purchase, 5, 0, [UIColor clearColor]);
        [self.purchase addTarget:self action:@selector(purchaseAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.contentView.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }
    [self.head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(10);
        make.width.height.offset(50).priorityHigh();
    }];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.head.mas_right).offset(5);
        make.top.equalTo(self.head).offset(5);
    }];
    
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.name);
        make.bottom.equalTo(self.head).offset(-5);
    }];
    
    [self.addFriends mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.width.offset(80);
        make.height.offset(25);
        make.centerY.equalTo(self.head);
    }];
    
    
    self.title.preferredMaxLayoutWidth = SCREEN_WIDTH-20;
    [self.title setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    self.title.numberOfLines =0;
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.head.mas_bottom).offset(10).priorityHigh();
        make.left.offset(10).priorityHigh();
        make.width.offset(SCREEN_WIDTH-20);
    }];
    
    self.text.preferredMaxLayoutWidth = SCREEN_WIDTH-20;
    [self.text setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    self.text.numberOfLines=0;
    [self.text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.title.mas_bottom).offset(5);
        make.left.offset(10).priorityHigh();
        make.width.offset(SCREEN_WIDTH-20);
    }];
   
    NSInteger count=0;
    if (self.model.pics.count==0) {
        count=0;
    }else if(self.model.pics.count>0&&self.model.imageArray.count<=3){
        count=1;
    }else{
        count=2;
    }
    [self.imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.text.mas_bottom).offset(10);
        make.left.offset(10);
        make.width.offset(SCREEN_WIDTH-20);
        //        make.height.offset(200).priorityHigh();
        make.height.offset(IMGSIZE*count+(count-1)*10).priorityHigh();
    }];
    
    
    [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView.mas_bottom).offset(20);
        make.left.right.offset(0);
        make.height.offset(0.5);
    }];
    
    [self.like mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom).offset(0);
        make.left.offset(20);
        make.height.offset(40);
//        make.width.offset(300);
        make.bottom.offset(0).priorityHigh();
    }];
    [self.like layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    
    [self.purchase mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.width.offset(55);
        make.centerY.equalTo(self.like);
    }];

}
-(void)setModel:(ShareModel *)model{
    _model=model;
    NSLog(@"%lu",(unsigned long)self.model.pics.count);
    self.name.text=model.member_id;
    [self.head sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMGURL,model.member_image]] placeholderImage:[UIImage imageNamed:@"图片空白页"]];
    self.time.text=[PublicClass timeWithTimeIntervalString:model.register_date];
    self.text.text=model.comment;
    self.title.text=model.goods_name;
    [self.like setTitle:[NSString stringWithFormat:@"%@ %@",model.love,Localized(@"人喜欢")] forState:UIControlStateNormal];
    self.like.selected=model.like;
    self.addFriends.selected= [model.is_friend isEqualToString:@"0"]? NO : YES;
    ViewBorderRadius(self.addFriends, 5, 1, [model.is_friend isEqualToString:@"0"]? [UIColor orangeColor] : [UIColor colorWithHexString:@"#adadad"]);
    while ([self.imgView.subviews lastObject] != nil) {
        [(UIView*)[self.imgView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
    }
    for (int i=0; i<self.model.pics.count; i++) {
        UIImageView *imageView =[[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMGURL,self.model.pics[i]]] placeholderImage:[UIImage imageNamed:@"图片空白页"]];
        imageView.contentMode=UIViewContentModeScaleAspectFill;
        ViewBorderRadius(imageView, 3, 0.5, [UIColor colorWithHexString:@"#adadad"]);
        [self.imgView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(IMGSIZE*(i%3)+(i%3)*10);
            make.top.offset(IMGSIZE*(i/3)+(i/3)*10);
            make.width.height.offset(IMGSIZE);
        }];
    }
}
-(void)addFriendsAction{
//    if (![self.model.is_friend isEqualToString:@"0"]) {
//        return;
//    }
    if (self.delegate&&[self.delegate respondsToSelector:@selector(addFriends:)]) {
        [self.delegate addFriends:self];
    }
}
-(void)purchaseAction{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(purchase:)]) {
        [self.delegate purchase:self];
    }
}
-(void)likeAction:(UIButton *)sender{
    NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
    NSMutableArray *likeArray;
    id obj=[ud objectForKey:@"like"];
    if (obj)
    {
        likeArray=(NSMutableArray *)obj;
    }
    if (!sender.selected) {
        sender.selected=YES;
        NSInteger like=[sender.titleLabel.text intValue];;
        like++;
        [self.like setTitle:[NSString stringWithFormat:@"%ld %@",(long)like,Localized(@"人喜欢")] forState:UIControlStateNormal];
    }
   
    if ([likeArray containsObject:self.model.uid]) {
        NSLog(@"你已经点过啦");
        return;
    }

    
    if (self.delegate&&[self.delegate respondsToSelector:@selector(likeAction:)]) {
        [self.delegate likeAction:self];
    }
}
+ (CGFloat)calculateHeight:(ShareModel*)model{
    NSInteger count=0;
    if (model.pics.count==0) {
        count=0;
    }else if(model.pics.count>0&&model.pics.count<=3){
        count=1;
    }else{
        count=2;
    }
    
    CGSize titleSize = [model.goods_name boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    
    CGSize textSize = [model.comment boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
    
    return 70+IMGSIZE*count+(count-1)*10+textSize.height+titleSize.height+35+40;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
